/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PickerPanel.java
 *
 * Created on Jun 27, 2012, 9:36:31 AM
 */

package com.piza.datepicker;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;

/**
 *
 * @author cwb
 */
public class PickerPanel extends javax.swing.JPanel {

    private JDialog dialog;
    private final String[] allMonth=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12"};
    private static final String[] allYears=new String[20];
    static {
               int startYear=2002;
                for(int i =0;i<20;i++){
                    allYears[i]=""+(startYear+i);
                }

            }
    /** Creates new form PickerPanel */
    public PickerPanel() {
        initComponents();
        this.allMonthComboBox.setModel(new DefaultComboBoxModel(allMonth));
          allYearComboBox.setModel(new DefaultComboBoxModel(allYears));
    }

    public void setParent(JDialog dialog){
        this.dialog=dialog;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        datePanel = new com.piza.datepicker.DatePanel();
        monthPanel = new javax.swing.JPanel();
        allMonthComboBox = new javax.swing.JComboBox();
        preMonthButton = new javax.swing.JButton();
        nextMonthButton = new javax.swing.JButton();
        allYearComboBox = new javax.swing.JComboBox();
        closeButton = new javax.swing.JButton();
        todayButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setName("Form"); // NOI18N

        datePanel.setName("datePanel"); // NOI18N

        monthPanel.setName("monthPanel"); // NOI18N

        allMonthComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        allMonthComboBox.setName("allMonthComboBox"); // NOI18N
        allMonthComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allMonthComboBoxActionPerformed(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PickerPanel.class);
        preMonthButton.setText(resourceMap.getString("preMonthButton.text")); // NOI18N
        preMonthButton.setName("preMonthButton"); // NOI18N
        preMonthButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                preMonthButtonActionPerformed(evt);
            }
        });

        nextMonthButton.setText(resourceMap.getString("nextMonthButton.text")); // NOI18N
        nextMonthButton.setName("nextMonthButton"); // NOI18N
        nextMonthButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextMonthButtonActionPerformed(evt);
            }
        });

        allYearComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        allYearComboBox.setName("allYearComboBox"); // NOI18N
        allYearComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allYearComboBoxActionPerformed(evt);
            }
        });

        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout monthPanelLayout = new javax.swing.GroupLayout(monthPanel);
        monthPanel.setLayout(monthPanelLayout);
        monthPanelLayout.setHorizontalGroup(
            monthPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(monthPanelLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(preMonthButton)
                .addGap(18, 18, 18)
                .addComponent(allMonthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(allYearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(nextMonthButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addComponent(closeButton)
                .addGap(25, 25, 25))
        );
        monthPanelLayout.setVerticalGroup(
            monthPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(monthPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(monthPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(preMonthButton)
                    .addComponent(allMonthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nextMonthButton)
                    .addComponent(allYearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(closeButton))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        todayButton.setText(resourceMap.getString("todayButton.text")); // NOI18N
        todayButton.setName("todayButton"); // NOI18N
        todayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                todayButtonActionPerformed(evt);
            }
        });

        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(137, 137, 137)
                .addComponent(todayButton)
                .addGap(33, 33, 33)
                .addComponent(okButton)
                .addContainerGap(142, Short.MAX_VALUE))
            .addComponent(monthPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(datePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(monthPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(datePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(todayButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void preMonthButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_preMonthButtonActionPerformed
        int currentIndex=this.allMonthComboBox.getSelectedIndex();
        if(currentIndex>0){
            this.allMonthComboBox.setSelectedIndex(currentIndex-1);
            timeChanged();
        }
    }//GEN-LAST:event_preMonthButtonActionPerformed

    private void allMonthComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allMonthComboBoxActionPerformed
         timeChanged();
    }//GEN-LAST:event_allMonthComboBoxActionPerformed

    private void nextMonthButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextMonthButtonActionPerformed
       int currentIndex=this.allMonthComboBox.getSelectedIndex();
        if(currentIndex< (this.allMonth.length-1)){
            this.allMonthComboBox.setSelectedIndex(currentIndex+1);
            timeChanged();
        }
    }//GEN-LAST:event_nextMonthButtonActionPerformed

    private void allYearComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allYearComboBoxActionPerformed
        timeChanged();
    }//GEN-LAST:event_allYearComboBoxActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        if(this.dialog!=null){
            dialog.setVisible(false);
        }
    }//GEN-LAST:event_closeButtonActionPerformed

    private void todayButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_todayButtonActionPerformed
        this.datePanel.toToday();
    }//GEN-LAST:event_todayButtonActionPerformed

    private void timeChanged(){
        int newMonth=Integer.valueOf(this.allMonthComboBox.getSelectedItem().toString());
        
        int newYear= Integer.valueOf(this.allYearComboBox.getSelectedItem().toString());

        this.datePanel.updateTime(newYear,newMonth);
        this.datePanel.revalidate();
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox allMonthComboBox;
    private javax.swing.JComboBox allYearComboBox;
    private javax.swing.JButton closeButton;
    private com.piza.datepicker.DatePanel datePanel;
    private javax.swing.JPanel monthPanel;
    private javax.swing.JButton nextMonthButton;
    private javax.swing.JButton okButton;
    private javax.swing.JButton preMonthButton;
    private javax.swing.JButton todayButton;
    // End of variables declaration//GEN-END:variables

}
